package top.tangyh.lamp.care.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BHl7MoveDetail;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.result.BHl7MoveDetailResultVO;
import top.tangyh.lamp.care.vo.result.RkCkInfoOutVO;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * 归转记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 13:49:07
 * @create [2024-07-15 13:49:07] [zqb] [代码生成器生成]
 */
@Repository
public interface BHl7MoveDetailMapper extends SuperMapper<BHl7MoveDetail> {


    List<BHl7MoveDetailResultVO> selectRkListOfHistoryTb(String hospitalNumber);

    default List<BHl7MoveDetail> selectListByHospitalNumbers(List<String> hospitalNumbers) {
        LambdaQueryWrapper<BHl7MoveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7MoveDetail::getIsDeleted, 0)
                .in(BHl7MoveDetail::getHospitalNumber, hospitalNumbers);
        return this.selectList(queryWrapper);
    }

    default List<BHl7MoveDetail> selectListByHospitalNumber(String hospitalNumber) {
        LambdaQueryWrapper<BHl7MoveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7MoveDetail::getIsDeleted, 0)
                .eq(BHl7MoveDetail::getHospitalNumber, hospitalNumber);
        return this.selectList(queryWrapper);
    }


    default void updateZcTime(String deptCode, Date eventTime) {
        LambdaQueryWrapper<BHl7MoveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7MoveDetail::getDeptIdent, deptCode)
                .orderByDesc(BHl7MoveDetail::getZrTime)
                .last("limit 1");
        BHl7MoveDetail yxHl7MoveDetailPO = new BHl7MoveDetail();
        yxHl7MoveDetailPO.setZcTime(PDateUtils.dateToLocalDateTime(eventTime));
        this.update(yxHl7MoveDetailPO, queryWrapper);
    }

    /**
     * 查询最近一次出入科时间
     *
     * @param hospitalNumber 住院号
     * @return
     */
    List<RkCkInfoOutVO> queryLastRkCkTime(String icuCode, String hospitalNumber);


    /**
     * 统计转入EICU科室人数
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param deptCode  科室编码
     * @return
     */
    default int countZhuanRu(String startTime, String endTime, String deptCode) {
        LambdaQueryWrapper<BHl7MoveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7MoveDetail::getDeptIdent, deptCode)
                .ge(BHl7MoveDetail::getZrTime, startTime)
                .lt(BHl7MoveDetail::getZrTime, endTime);
        return Math.toIntExact(this.selectCount(queryWrapper));
    }

    /**
     * 统计转出EICU科室人数
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param deptCode  科室编码
     * @return
     */
    default int countZhuanChu(String startTime, String endTime, String deptCode) {
        LambdaQueryWrapper<BHl7MoveDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7MoveDetail::getOldDeptIdent, deptCode)
                .ge(BHl7MoveDetail::getZrTime, startTime)
                .lt(BHl7MoveDetail::getZrTime, endTime);
        return Math.toIntExact(this.selectCount(queryWrapper));
    }


    BHl7MoveDetailResultVO selectCyTimeRecordByHospitalNumber(LocalDateTime rkTime,String hospitalNumber);
}


